package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("requestCookie")
public class RequestCookieController {

    /**
     * 接收浏览器请求的Cookie数据
     * 注解 @CookieValue 获取浏览器的Cookie数据
     * 属性 value，配置的是浏览器中Cookie的键名
     */
    @RequestMapping("receiveCookie")
    public void receiveCookie(@CookieValue("zz") String cookieValue){
        System.out.println("cookieValue = " + cookieValue);
    }

    /**
     * 发送Cookie数据到浏览器
     */
    @RequestMapping("sendCookie")
    public void sendCoolie(HttpServletResponse response){
        Cookie cookie = new Cookie("zz","yyy");
        response.addCookie(cookie);
    }

    /**
     * 注解 @RequestHeader获取客户端浏览器的请求头
     * 注解属性value：配置请求头的键，获取请求头的值
     */
    @RequestMapping("requestHeader")
    public void requestHeader(@RequestHeader("User-Agent")String header){
        System.out.println("header = " + header);
    }
}
